home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / window.el < prev    next >
Encoding:
Text File  |  1995-08-11  |  11.7 KB  |  311 lines

  1. ;;; window.el --- XEmacs window commands aside from those written in C.
  2. ;; Keywords: extensions
  3.  
  4. ;; Copyright (C) 1985, 1989, 1993, 1994 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;; Synched up with: FSF 19.28.
  23.  
  24. ;;;; Window tree functions.
  25.  
  26. (defun one-window-p (&optional nomini all-frames)
  27.   "Returns non-nil if the selected window is the only window (in its frame).
  28. Optional arg NOMINI non-nil means don't count the minibuffer
  29. even if it is active.
  30.  
  31. The optional arg ALL-FRAMES t means count windows on all frames.
  32. If it is `visible', count windows on all visible frames.
  33. ALL-FRAMES nil or omitted means count only the selected frame, 
  34. plus the minibuffer it uses (which may be on another frame).
  35. ALL-FRAMES = 0 means count windows on all visible and iconified frames.
  36. If ALL-FRAMES is any other value, count only the selected frame."
  37.   (let ((base-window (selected-window)))
  38.     (if (and nomini (eq base-window (minibuffer-window)))
  39.     (setq base-window (next-window base-window)))
  40.     (eq base-window
  41.     (next-window base-window (if nomini 'arg) all-frames))))
  42.  
  43. (defun walk-windows (proc &optional minibuf all-frames)
  44.   "Cycle through all visible windows, calling PROC for each one.
  45. PROC is called with a window as argument.
  46. Optional second arg MINIBUF t means count the minibuffer window
  47. even if not active.  If MINIBUF is neither t nor nil it means
  48. not to count the minibuffer even if it is active.
  49.  
  50. Optional third arg ALL-FRAMES t means include all windows in all frames;
  51. otherwise cycle within the selected frame."
  52.   ;; If we start from the minibuffer window, don't fail to come back to it.
  53.   (if (window-minibuffer-p (selected-window))
  54.       (setq minibuf t))
  55.   ;; Note that, like next-window & previous-window, this behaves a little 
  56.   ;; strangely if the selected window is on an invisible frame: it hits
  57.   ;; some of the windows on that frame, and all windows on visible frames.
  58.   (let* ((walk-windows-history nil)
  59.      (walk-windows-current (selected-window)))
  60.     (while (progn
  61.          (setq walk-windows-current
  62.            (next-window walk-windows-current minibuf all-frames))
  63.        q  (not (memq walk-windows-current walk-windows-history)))
  64.       (setq walk-windows-history (cons walk-windows-current
  65.                        walk-windows-history))
  66.       (funcall proc walk-windows-current))))
  67.  
  68.  
  69. (defun count-windows (&optional minibuf)
  70.    "Returns the number of visible windows.
  71. Optional arg NO-MINI non-nil means don't count the minibuffer
  72. even if it is active."
  73.    (let ((count 0))
  74.      (walk-windows (function (lambda (w)
  75.                    (setq count (+ count 1))))
  76.            minibuf)
  77.      count))
  78.  
  79. (defun balance-windows ()
  80.   "Makes all visible windows the same height (approximately)."
  81.   (interactive)
  82.   (let ((count -1) levels newsizes size)
  83.     ;; Find all the different vpos's at which windows start,
  84.     ;; then count them.  But ignore levels that differ by only 1.
  85.     (save-window-excursion
  86.       (let (tops (prev-top -2))
  87.     (walk-windows (function (lambda (w)
  88.                         (setq tops (cons (nth 1 (window-pixel-edges w))
  89.                                          tops))))
  90.               'nomini)
  91.     (setq tops (sort tops '<))
  92.     (while tops
  93.       (if (> (car tops) (1+ prev-top))
  94.           (setq prev-top (car tops)
  95.             count (1+ count)))
  96.       (setq levels (cons (cons (car tops) count) levels))
  97.       (setq tops (cdr tops)))
  98.     (setq count (1+ count))))
  99.     ;; Subdivide the frame into that many vertical levels.
  100.     (setq size (/ (frame-pixel-height) count))
  101.     (walk-windows (function (lambda (w)
  102.                     (select-window w)
  103.                     (let ((newtop (cdr (assq (nth 1 (window-pixel-edges))
  104.                                              levels)))
  105.                           (newbot (or (cdr (assq
  106.                         (+ (window-pixel-height)
  107.                            (nth 1 (window-pixel-edges)))
  108.                         levels))
  109.                                       count)))
  110.                       (setq newsizes
  111.                             (cons (cons w (* size (- newbot newtop)))
  112.                                   newsizes))))))
  113.     (walk-windows (function (lambda (w)
  114.                   (select-window w)
  115.                   (let ((newsize (cdr (assq w newsizes))))
  116.                 (enlarge-window
  117.                  (/ (- newsize (window-pixel-height))
  118.                     (face-height 'default))))))
  119.                   'nomini)))
  120.  
  121. (defvar split-window-keep-point t
  122.   "*If non-nil, split windows keeps the original point in both children.
  123. This is often more convenient for editing.
  124. If nil, adjust point in each of the two windows to minimize redisplay.
  125. This is convenient on slow terminals, but point can move strangely.")
  126.  
  127. (defun split-window-vertically (&optional arg)
  128.   "Split current window into two windows, one above the other.
  129. The uppermost window gets ARG lines and the other gets the rest.
  130. Negative arg means select the size of the lowermost window instead.
  131. With no argument, split equally or close to it.
  132. Both windows display the same buffer now current.
  133.  
  134. If the variable split-window-keep-point is non-nil, both new windows
  135. will get the same value of point as the current window.  This is often
  136. more convenient for editing.
  137.  
  138. Otherwise, we chose window starts so as to minimize the amount of
  139. redisplay; this is convenient on slow terminals.  The new selected
  140. window is the one that the current value of point appears in.  The
  141. value of point can change if the text around point is hidden by the
  142. new modeline.
  143.  
  144. Programs should probably use split-window instead of this."
  145.   (interactive "P")
  146.   (let ((old-w (selected-window))
  147.     (old-point (point))
  148.     (size (and arg (prefix-numeric-value arg)))
  149.     new-w bottom)
  150.     (and size (< size 0) (setq size (+ (window-height) size)))
  151.     (setq new-w (split-window nil size))
  152.     (or split-window-keep-point
  153.     (progn
  154.       (save-excursion
  155.         (set-buffer (window-buffer))
  156.         (goto-char (window-start))
  157.         (vertical-motion (window-height))
  158.         (set-window-start new-w (point))
  159.         (if (> (point) (window-point new-w))
  160.         (set-window-point new-w (point)))
  161.         (vertical-motion -1)
  162.         (setq bottom (point)))
  163.       (if (<= bottom (point))
  164.           (set-window-point old-w (1- bottom)))
  165.       (if (< (window-start new-w) old-point)
  166.           (progn
  167.         (set-window-point new-w old-point)
  168.         (select-window new-w)))))
  169.     new-w))
  170.  
  171. (defun split-window-horizontally (&optional arg)
  172.   "Split current window into two windows side by side.
  173. This window becomes the leftmost of the two, and gets ARG columns.
  174. Negative arg means select the size of the rightmost window instead.
  175. No arg means split equally."
  176.   (interactive "P")
  177.   (let ((size (and arg (prefix-numeric-value arg))))
  178.     (and size (< size 0)
  179.      (setq size (+ (window-width) size)))
  180.     (split-window nil size t)))
  181.  
  182. (defun enlarge-window-horizontally (arg)
  183.   "Make current window ARG columns wider."
  184.   (interactive "p")
  185.   (enlarge-window arg t))
  186.  
  187. (defun shrink-window-horizontally (arg)
  188.   "Make current window ARG columns narrower."
  189.   (interactive "p")
  190.   (shrink-window arg t))
  191.  
  192. (defun shrink-window-if-larger-than-buffer (&optional window)
  193.   "Shrink the WINDOW to be as small as possible to display its contents.
  194. Do not shrink to less than `window-min-height' lines.
  195. Do nothing if the buffer contains more lines than the present window height,
  196. or if some of the window's contents are scrolled out of view,
  197. or if the window is not the full width of the frame,
  198. or if the window is the only window of its frame."
  199.   (interactive)
  200.   (save-excursion
  201.     (set-buffer (window-buffer window))
  202.     (let ((w (selected-window)) ;save-window-excursion can't win
  203.       (buffer-file-name buffer-file-name)
  204.       (p (point))
  205.       (n 0)
  206.       (ignore-final-newline
  207.        ;; If buffer ends with a newline, ignore it when counting height
  208.        ;; unless point is after it.
  209.        (and (not (eobp))
  210.         (eq ?\n (char-after (1- (point-max))))))
  211.       (buffer-read-only nil)
  212.       (modified (buffer-modified-p))
  213.       (buffer (current-buffer)))
  214.       (if (and (< 1 (count-windows))
  215.            ;; check to make sure that we don't have horizontally
  216.            ;; split windows
  217.            (eq (frame-highest-window (selected-frame) 0)
  218.            (frame-highest-window (selected-frame) -1))
  219.            (pos-visible-in-window-p (point-min) window)
  220.            ;; #### there needs to be a check here for a global
  221.            ;; minibuffer
  222.            )
  223.       (unwind-protect
  224.           (progn
  225.         (select-window (or window w))
  226.         (goto-char (point-min))
  227.         (while (pos-visible-in-window-p
  228.             (- (point-max)
  229.                (if ignore-final-newline 1 0)))
  230.           ;; defeat file locking... don't try this at home, kids!
  231.           (setq buffer-file-name nil)
  232.           (insert ?\n) (setq n (1+ n)))
  233.         (if (> n 0)
  234.             (shrink-window (min (1- n)
  235.                     (- (window-height)
  236.                        window-min-height)))))
  237.         (delete-region (point-min) (point))
  238.         (set-buffer-modified-p modified)
  239.         (goto-char p)
  240.         (select-window w)
  241.         ;; Make sure we unbind buffer-read-only
  242.         ;; with the proper current buffer.
  243.         (set-buffer buffer))))))
  244.  
  245. (defun backward-other-window (arg &optional all-frames invisible-too)
  246.   "Select the ARG'th different window on this frame, going backwards.
  247. This is just like calling `other-window' with the arg negated."
  248.   (interactive "p")
  249.   (other-window (- arg) all-frames invisible-too))
  250.  
  251. (defun windows-of-buffer (&optional buffer)
  252.   "Returns a list of windows that have BUFFER in them.
  253. If BUFFER is not specified, the current buffer will be used."
  254.   (or (bufferp buffer)
  255.       (if (stringp buffer)
  256.       (setq buffer (or (get-buffer buffer)
  257.                (get-file-buffer buffer)))
  258.     (setq buffer (current-buffer))))
  259.   (let* ((firstwin (next-window nil nil t))
  260.      (wind firstwin) 
  261.      (done nil)
  262.      window-list)
  263.     (while (not done)
  264.       (if (eq (window-buffer wind) buffer)
  265.       (setq window-list (append window-list (list wind))))
  266.       (setq wind (next-window wind nil t))
  267.       (setq done (eq wind firstwin)))
  268.     window-list))
  269.  
  270. (defun buffer-in-multiple-windows-p (&optional buffer)
  271.   "Returns t if BUFFER is in multiple windows.
  272. If BUFFER is not specified, the current buffer will be used."
  273.   (setq buffer (or buffer
  274.            (get-buffer buffer)
  275.            (get-file-buffer buffer)
  276.            (current-buffer)))
  277.   (> (length (windows-of-buffer buffer)) 1))
  278.  
  279. (defun window-list (&optional frame minibuf window)
  280.   "Return a list of windows on FRAME, beginning with WINDOW.
  281. FRAME and WINDOW default to the selected ones.  
  282. Optional second arg MINIBUF t means count the minibuffer window
  283. even if not active.  If MINIBUF is neither t nor nil it means
  284. not to count the minibuffer even if it is active."
  285.   (setq window (or window (selected-window))
  286.     frame (or frame (selected-frame)))
  287.   (if (not (eq (window-frame window) frame))
  288.       (error "Window must be on frame."))
  289.   (let ((current-frame (selected-frame))
  290.     list)
  291.     (unwind-protect
  292.     (save-window-excursion
  293.       (select-frame frame)
  294.       (walk-windows
  295.        (function (lambda (cur-window)
  296.                (if (not (eq window cur-window))
  297.                (setq list (cons cur-window list)))))
  298.        minibuf)
  299.       (setq list (cons window list)))
  300.       (select-frame current-frame))))
  301.  
  302. ;; FSF compatibility function
  303. (defun set-window-dedicated-p (window arg)
  304.   "Control whether WINDOW is dedicated to the buffer it displays.
  305. If it is dedicated, Emacs will not automatically change
  306. which buffer appears in it.
  307. The second argument is the new value for the dedication flag;
  308. non-nil means yes."
  309.   (let ((buffer (if arg (window-buffer window) nil)))
  310.     (set-window-buffer-dedicated window buffer)))
  311.